package com.chao.deeply.forkjoin;

import lombok.SneakyThrows;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

/**
 * @author : Rookie
 * @description :
 * @date : 2023/5/13
 */
public class AddTest {

    public static void main(String[] args) {
        long startValue = 0;
        long endValue = 10_0000_0000;
        test1(startValue,endValue);
        test2(startValue,endValue);
        test3(startValue,endValue);
    }

    public static void test1(long startValue,long endValue) {
        long res = 0;
        long startTime = System.currentTimeMillis();
        for (long i = startValue;i <= endValue;i++) {
            res += i;
        }
        System.out.println("test1执行完成，最终结果为 " + res + "花费时间 " + (System.currentTimeMillis() - startTime) + " 毫秒");
    }

    @SneakyThrows
    public static void test2(long startValue, long endValue) {
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinDemo.MyForkJoinTask forkJoinTask = new ForkJoinDemo.MyForkJoinTask(startValue, endValue);
        long startTime = System.currentTimeMillis();
        ForkJoinTask<Long> resTask = forkJoinPool.submit(forkJoinTask);
        System.out.println("test2执行完成，最终结果为 " + resTask.get() + "花费时间 " + (System.currentTimeMillis() - startTime) + " 毫秒");
    }

    public static void test3(long startValue,long endValue) {
        long startTime = System.currentTimeMillis();
        long res = LongStream.range(startValue, endValue + 1).reduce(Long::sum).getAsLong();
        System.out.println("test3执行完成，最终结果为 " + res + "花费时间 " + (System.currentTimeMillis() - startTime) + " 毫秒");
    }
}
